﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BonSaiTableAdapters;
using System.Security;
using System.Text;
using System.Security.Cryptography;


public partial class admin_AdminDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            if (Request.QueryString["adminID"]!= null)
            {
                try
                {
                    int adminID = Int32.Parse(Request.QueryString["adminID"].ToString());
                    ViewState["adminID"] = adminID;
                    LoadMemberInfo();
                 
                }
                catch (Exception ex)
                {
                    Response.Redirect("AdminManage.aspx");
                }
            }
            else
            {
                lblHeader.Text = "Tạo mới Admin";
                txtLoginName.Enabled = true;
                txtLoginName.ReadOnly = false;
                btnUpdate.Text = "Tạo mới";
            }
        }
    }

    private void LoadMemberInfo() 
    {
        if (ViewState["adminID"] != null)
        {
            int adminID = Int32.Parse(ViewState["adminID"].ToString());
            AdminsTableAdapter adminDA = new AdminsTableAdapter();
            BonSai.AdminsDataTable adminDT = adminDA.GetAdminByID(adminID);

            if (adminDT.Rows.Count > 0)
            {
                BonSai.AdminsRow adminRow = adminDT.Rows[0] as BonSai.AdminsRow;

                txtAdminID.Text = adminID.ToString();

                if (!adminRow.IsLoginNameNull())
                    txtLoginName.Text = adminRow.LoginName;
                if (!adminRow.IsFullNameNull())
                    txtFullName.Text = adminRow.FullName;
                if (!adminRow.IsRoleStatusNull())
                    ddlStatus.SelectedValue = adminRow.RoleStatus.ToString();

                if (adminRow.LoginName == "root")
                {
                    ddlStatus.Enabled = true;
                }
            }
            else
            {
                Response.Redirect("AdminManage.aspx");
            }
        }
        else
        {
            
            
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdminManage.aspx");  
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
            return;

        string fullName = txtFullName.Text.Trim();
        string loginName = txtLoginName.Text.Trim();
        int status = Int32.Parse(ddlStatus.SelectedValue.ToString());
        AdminsTableAdapter adminDA = new AdminsTableAdapter();

        if (ViewState["adminID"] != null)
        {
            /////Update admin

            int adminID = Int32.Parse(ViewState["adminID"].ToString());
            adminDA.UpdateProfileAdmin(loginName, fullName, status, adminID);
            Response.Redirect("AdminManage.aspx?code = 2");
        }
        else
        {
            BonSai.AdminsDataTable adminDT = adminDA.GetAdminByLoginName(loginName);
            if (adminDT.Rows.Count > 0) 
            {
                lblTextMessage.Text = "Tên đăng nhập đã tồn tại !!!";
                return;
            }

            string pass = "123456";
            ASCIIEncoding ascEn = new ASCIIEncoding();
            SHA512Managed sha512 = new SHA512Managed();
            byte[] bInput = ascEn.GetBytes(pass);
            string encodedPass = ascEn.GetString(sha512.ComputeHash(bInput));

            adminDA.Insert(loginName, encodedPass, fullName, status);

            Response.Redirect("AdminManage.aspx?code=1");
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {

    }
}
